package com.neo.rabbit;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DirectRabbitMQConfig
{
    public static final String DIRECT_EXCHANGE = "directExchange";
    
    public static final String QUEUE_DIRECT_A = "direct.A";
    
    public static final String QUEUE_DIRECT_B = "direct.B";
    
    // 创建一个direct交换机
    @Bean
    DirectExchange directExchange()
    {
        return new DirectExchange(DIRECT_EXCHANGE);
    }
    
    // 创建队列A
    @Bean
    Queue queueDirectNameA()
    {
        return new Queue(QUEUE_DIRECT_A);
    }
    
    // 创建队列B
    @Bean
    Queue queueDirectNameB()
    {
        return new Queue(QUEUE_DIRECT_B);
    }
    
    // 将direct.A队列绑定到directExchange交换机中，使用direct.a.key作为路由规则
    @Bean
    Binding bindingExchangeMessageDirectA(Queue queueDirectNameA, DirectExchange directExchange)
    {
        return BindingBuilder.bind(queueDirectNameA).to(directExchange).with("direct.a.key");
    }
    
    // 将direct.B队列绑定到directExchange交换机中，使用direct.b.key作为路由规则
    @Bean
    Binding bindingExchangeMessageDirectB(Queue queueDirectNameB, DirectExchange directExchange)
    {
        return BindingBuilder.bind(queueDirectNameB).to(directExchange).with("direct.b.key");
    }
}